package com.lmf.mock.starter.autoconfigure.controller;

import com.lmf.mock.starter.autoconfigure.entity.MockApi;
import com.lmf.mock.starter.autoconfigure.service.MockApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Mock接口管理控制器
 */
@RestController
@RequestMapping("/mock/api")
public class MockApiController {

    @Autowired
    private MockApiService mockApiService;

    /**
     * 获取所有接口
     */
    @GetMapping("/list")
    public ResponseEntity<List<MockApi>> getAllApis() {
        List<MockApi> apis = mockApiService.list();
        return ResponseEntity.ok(apis);
    }

    /**
     * 根据分组ID获取接口
     */
    @GetMapping("/group/{groupId}")
    public ResponseEntity<List<MockApi>> getApisByGroupId(@PathVariable Long groupId) {
        List<MockApi> apis = mockApiService.getApisByGroupId(groupId);
        return ResponseEntity.ok(apis);
    }

    /**
     * 根据ID获取接口
     */
    @GetMapping("/{id}")
    public ResponseEntity<MockApi> getApiById(@PathVariable Long id) {
        MockApi api = mockApiService.getById(id);
        if (api != null) {
            return ResponseEntity.ok(api);
        }
        return ResponseEntity.notFound().build();
    }

    /**
     * 创建接口
     */
    @PostMapping
    public ResponseEntity<Boolean> createApi(@RequestBody MockApi api) {
        boolean result = mockApiService.createApi(api);
        return ResponseEntity.ok(result);
    }

    /**
     * 更新接口
     */
    @PutMapping("/{id}")
    public ResponseEntity<Boolean> updateApi(@PathVariable Long id, @RequestBody MockApi api) {
        api.setId(id);
        boolean result = mockApiService.updateApi(api);
        return ResponseEntity.ok(result);
    }

    /**
     * 删除接口
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Boolean> deleteApi(@PathVariable Long id) {
        boolean result = mockApiService.deleteApi(id);
        return ResponseEntity.ok(result);
    }

    /**
     * 切换接口状态
     */
    @PutMapping("/{id}/status")
    public ResponseEntity<Boolean> toggleApiStatus(@PathVariable Long id, @RequestParam Boolean enabled) {
        boolean result = mockApiService.toggleApiStatus(id, enabled);
        return ResponseEntity.ok(result);
    }
}
